package com.dalei.vds.graphics;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

public class ImageUtils {
	
	public static boolean compressImage(File from, File to, float scale, CompressFormat format)
			throws FileNotFoundException, IOException {
		if (from != null || scale > 0) {
			InputStream in = new FileInputStream(from);
			Bitmap source = BitmapFactory.decodeStream(in);

			Matrix matrix = new Matrix();
			matrix.postScale(scale, scale);

			Bitmap target = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
			
			if (to.createNewFile() == false) {
				return false;
			}
			OutputStream out = new FileOutputStream(to);
			boolean result = target.compress(format, 100, out);
			out.close();
			
			in.close();
			return result;
		} else {
			return false;
		}
	}
}
